package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月30日 13:45
 */
public class GoodsServiceimpl implements GoodsService {
    @Override
    public void add(MarketGoods goods) {
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketGoodsMapper mapper = sqlSession.getMapper(MarketGoodsMapper.class);
        mapper.insertSelective(goods);
    }

    @Override
    public CommonData query(Integer page, Integer limit, String sort, String order, String goodsId, String goodsSn, String name) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketGoodsMapper marketGoodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        // 分页
        PageHelper.startPage(page, limit);

        MarketGoodsExample example = new MarketGoodsExample();
        // 排序
        example.setOrderByClause(sort + " " + order);


        // 条件的集合  id：精确匹配  编号：精确匹配  名称：模糊匹配
        //  goodsId     goodsSn   name
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        //goodsId 对应 brandId
        if (goodsId != null && goodsId.length() > 0) {
            criteria.andIdEqualTo(Integer.valueOf(goodsId));
        }

        if (goodsSn != null && goodsSn.length() > 0) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }

        if (name != null && name.length() > 0) {
            criteria.andNameLike("%" + name + "%");
        }

        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExampleWithBLOBs(example);
        PageInfo pageInfo = new PageInfo(marketGoodsList);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketGoods selectById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketGoodsMapper mapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketGoods marketGoods = mapper.selectByPrimaryKey(id);

        return marketGoods;

    }

    @Override
    public void update(MarketGoods goods) {
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketGoodsMapper mapper = sqlSession.getMapper(MarketGoodsMapper.class);
        mapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public void delete(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketGoodsMapper mapper = sqlSession.getMapper(MarketGoodsMapper.class);
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<MarketGoods> selectAll() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketGoodsMapper marketGoodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        MarketGoodsExample.Criteria criteria1 = criteria.andIdGreaterThan(0);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
        return marketGoods;
    }

    @Override
    public MarketGoods selectOne(Integer goodsId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketGoodsMapper mapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketGoods marketGoods = mapper.selectByPrimaryKey(goodsId);
        return marketGoods;
    }

}
